from django.conf import settings


class webAttr:
    """网站属性配置 1"""

    def __init__(self):
        self.id = 1
        self.name = None
        self.logo = None
        self.width = None
        self.height = None
        self.idc = None
        self.callme = None
        self.status = 0
        self.is_active = False
        self.readConf()

    def readConf(self):
        try:
            with open(settings.CONF / "attrs.json", "r") as fs:
                Secret = eval(fs.read())
            self.name = Secret.get("name")
            self.logo = Secret.get("logo")
            self.width = Secret.get("width")
            self.height = Secret.get("height")
            self.icp = Secret.get("icp")
            self.callme = Secret.get("callme")
            self.status = Secret.get("status")
            self.is_active = True
        except BaseException as err:
            self.is_active = False

    def to_json(self):
        if not self.is_active:
            return None
        return {
            "name": self.name,
            "logo": self.logo,
            "width": self.width,
            "height": self.height,
            "icp": self.icp,
            "callme": self.callme,
            "status": self.status,
        }

    def up_json(self, attr):
        try:
            with open(settings.CONF / "attrs.json", "w") as fs:
                fs.write(f"{attr}")
        except BaseException as err:
            print(err)
        finally:
            self.readConf()


class Receipt:
    """收单参数配置 2"""

    def __init__(self):
        # print("初始化收单参数")
        self.id = 2
        self.name = None
        self.appid = None
        self.shopid = None
        self.bnkpublic = None
        self.rsapublic = None
        self.rsaprivate = None
        self.interface = None
        self.notify = None
        self.status = 0
        self.is_active = False
        self.readConf()

    def readConf(self):
        # print("加载收单参数配置")
        try:
            with open(settings.CONF / "allinpay.json", "r") as fs:
                Secret = eval(fs.read())
            self.name = Secret.get("name")
            self.appid = Secret.get("appid")
            self.shopid = Secret.get("shopid")
            self.bnkpublic = Secret.get("bnkpublic")
            self.rsapublic = Secret.get("rsapublic")
            self.rsaprivate = Secret.get("rsaprivate")
            self.interface = Secret.get("interface")
            self.notify = Secret.get("notify")
            self.status = Secret.get("status")
            self.is_active = True
        except BaseException as err:
            # print(err)
            self.is_active = False

    def to_json(self):
        if not self.is_active:
            return None
        return {
            "name": self.name,
            "appid": self.appid,
            "shopid": self.shopid,
            "bnkpublic": self.bnkpublic,
            "rsaprivate": self.rsaprivate,
            "rsapublic": self.rsapublic,
            "interface": self.interface,
            "notify": self.notify,
            "status": self.status,
        }

    def up_json(self, attr):
        try:
            with open(settings.CONF / "allinpay.json", "w") as fs:
                fs.write(f"{attr}")
        except BaseException as err:
            print(err)
        finally:
            self.readConf()


class semsConf:
    """短信网关配置 3"""

    def __init__(self):
        self.id = 3
        self.name = None
        self.signname = None
        self.secretid = None
        self.secretkey = None
        self.sessdkappid = None
        self.templateid = None
        self.status = 0
        self.is_active = False
        self.readConf()

    def readConf(self):
        try:
            with open(settings.CONF / "sems.json", "r") as fs:
                Secret = eval(fs.read())
            self.name = Secret.get("name")
            self.signname = Secret.get("signname")
            self.secretid = Secret.get("secretid")
            self.secretkey = Secret.get("secretkey")
            self.sessdkappid = Secret.get("sessdkappid")
            self.templateid = Secret.get("templateid")
            self.status = Secret.get("status")
            self.is_active = True
        except BaseException as err:
            self.is_active = False

    def to_json(self):
        if not self.is_active:
            return None
        return {
            "name": self.name,
            "signname": self.signname,
            "secretid": self.secretid,
            "secretkey": self.secretkey,
            "sessdkappid": self.sessdkappid,
            "templateid": self.templateid,
            "status": self.status,
        }

    def up_json(self, attr):
        try:
            with open(settings.CONF / "sems.json", "w") as fs:
                fs.write(f"{attr}")
        except BaseException as err:
            print(err)
        finally:
            self.readConf()


class mailConf:
    """邮件网关配置 4"""

    def __init__(self):
        self.id = 4
        self.name = None
        self.username = None
        self.password = None
        self.pop3 = None
        self.title = None
        self.status = 0
        self.is_active = False
        self.readConf()

    def readConf(self):
        try:
            with open(settings.CONF / "mail.json", "r") as fs:
                Secret = eval(fs.read())
            self.name = Secret.get("name")
            self.username = Secret.get("username")
            self.password = Secret.get("password")
            self.pop3 = Secret.get("pop3")
            self.title = Secret.get("title")
            self.status = Secret.get("status")
            self.is_active = True
        except BaseException as err:
            self.is_active = False

    def to_json(self):
        if not self.is_active:
            return None
        return {
            "name": self.name,
            "username": self.username,
            "password": self.password,
            "pop3": self.pop3,
            "title": self.title,
            "status": self.status,
        }

    def up_json(self, attr):
        try:
            with open(settings.CONF / "mail.json", "w") as fs:
                fs.write(f"{attr}")
        except BaseException as err:
            print(err)
        finally:
            self.readConf()


class settlConf:
    """结算属性配置 5"""

    def __init__(self):
        self.id = 5
        self.min_value = 10000
        self.status = 0
        self.is_active = False
        self.readConf()

    def readConf(self):
        try:
            with open(settings.CONF / "sums.json", "r") as fs:
                Secret = eval(fs.read())
            self.min_value = Secret.get("min_value")
            self.status = Secret.get("status")
            self.is_active = True
        except BaseException as err:
            self.is_active = False

    def to_json(self):
        if not self.is_active:
            return None
        return {"min_value": self.min_value, "status": self.status}

    def up_json(self, attr):
        try:
            with open(settings.CONF / "sums.json", "w") as fs:
                fs.write(f"{attr}")
        except BaseException as err:
            print(err)
        finally:
            self.readConf()


class paysConf:
    """分账属性配置 6"""

    def __init__(self):
        self.id = 6
        self.name = None
        self.ipaddr = None
        self.port = None
        self.bank = None
        self.card = None
        self.addr = None
        self.code = None
        self.idcode = None
        self.status = 0
        self.is_active = False
        self.readConf()

    def readConf(self):
        try:
            with open(settings.CONF / "pays.json", "r") as fs:
                Secret = eval(fs.read())
            self.name = Secret.get("name")
            self.ipaddr = Secret.get("ipaddr")
            self.port = Secret.get("port")
            self.bank = Secret.get("bank")
            self.card = Secret.get("card")
            self.addr = Secret.get("addr")
            self.code = Secret.get("code")
            self.idcode = Secret.get("idcode")
            self.status = Secret.get("status")
            self.is_active = True
        except BaseException as err:
            self.is_active = False

    def to_json(self):
        if not self.is_active:
            return None
        return {
            "name": self.name,
            "ipaddr": self.ipaddr,
            "port": self.port,
            "bank": self.bank,
            "card": self.card,
            "addr": self.addr,
            "code": self.code,
            "idcode": self.idcode,
            "status": self.status,
        }

    def up_json(self, attr):
        try:
            with open(settings.CONF / "pays.json", "w") as fs:
                fs.write(f"{attr}")
        except BaseException as err:
            print(err)
        finally:
            self.readConf()


class alertConf:
    """语音token 7"""

    def __init__(self):
        self.id = 7
        self.name = None
        self.host = None
        self.user = None
        self.key = None
        self.status = 0
        self.is_active = False
        self.readConf()

    def readConf(self):
        try:
            with open(settings.CONF / "alert.json", "r") as fs:
                Secret = eval(fs.read())
            self.name = Secret.get("name")
            self.host = Secret.get("host")
            self.user = Secret.get("user")
            self.key = Secret.get("key")
            self.status = Secret.get("status")
            self.is_active = True
        except BaseException as err:
            self.is_active = False

    def to_json(self):
        if not self.is_active:
            return None
        return {
            "name": self.name,
            "host": self.host,
            "user": self.user,
            "key": self.key,
            "status": self.status,
        }

    def up_json(self, attr):
        try:
            with open(settings.CONF / "alert.json", "w") as fs:
                fs.write(f"{attr}")
        except BaseException as err:
            print(err)
        finally:
            self.readConf()
